﻿using Minecraft.BLL;
using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall.Cmd
{
	public class MapDeblocking : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Map_MapDeblocking;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<MapDeblockingReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}
			if (req.depotMapId.IsNullOrWhiteSpace())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：depotMapId 参数为空）" });
				return;
			}

			//var csvDeoptMapInfo = CsvConfig.mapList.FirstOrDefault(m => m.Id == req.depotMapId);
			var csvDeoptMapInfo = CsvConfig.GetMod_MapInfoById(req.depotMapId);
			if (csvDeoptMapInfo == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：csvDeoptMapInfo 配置信息不存在）" });
				return;
			}
			if (!csvDeoptMapInfo.If_depot_Convert)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：mapid:【{csvDeoptMapInfo.Id}】不是补给站）" });
				return;
			}
			var map = session.minecraftSessionInfo.map;
			
			//判断 depotMapId 是否在可视范围内
			var IsWithinVisualField = map.IsWithinVisualField(req.depotMapId);
			if (!IsWithinVisualField)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：depotMapId 不在可视范围内）" });
				return;
			}

			if (map.GetUnlockedDepotIdList().Any(m => m == req.depotMapId))
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：depotMapId 已经解锁了）" });
				return;
			}
			map.TransformToUnlockedDepotIdsStr(req.depotMapId);
			//var deoptList = CsvConfig.mapList.Where(m => m.If_depot_Convert && map.GetWithinVisualRangeMapIdList().Any(k => k == m.Id)).ToList();
			var deoptList = CsvConfig.GetmapDict().Select(m => m.Value).Where(m => m.If_depot_Convert && map.GetWithinVisualRangeMapIdList().Any(k => k == m.Id)).ToList();
			if (deoptList == null || !deoptList.Any())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：deoptList 列表为空，可能服务器出错）" });
				return;
			}
			foreach (var deopt in deoptList)
			{
				map.TransformToUnlockedDepotIdsStr(deopt.Id);
			}
			var resp = new MapDeblockingResp
			{

			};
			session.Send(defCommand, req, resp);
		}
	}
}
